package org.rcy.controller;

import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.framework.utils.beans.JsonUtils;
import org.rcy.po.EmployType;
import org.rcy.po.HiringInfo;
import org.rcy.po.SettleType;
import org.rcy.service.HiringInfoService;
import org.rcy.vo.HiringInfoSaveReq;
import org.rcy.vo.InfoQueryCondition;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/info")
public class HiringInfoController {

	@Autowired
	private HiringInfoService infoService;

	@PostMapping("/publish")
	public ResultMessage publish(@RequestBody HiringInfoSaveReq req) throws IOException {
		HiringInfo info = new HiringInfo();
		fillPo(req,info);
		infoService.saveInfo(info);
		return ResultMessage.success();
	}

	private void fillPo(HiringInfoSaveReq req,HiringInfo info) {
		BeanUtils.copyProperties(req,info);
		info.setEmployType(EmployType.valueOf(req.getEmployType()));
		info.setSettleType(SettleType.valueOf(req.getSettleType()));
		info.setEmployNum(Integer.valueOf(req.getEmployNum()));
		info.setPublishTime(new Date());
		info.setUpdateTime(new Date());
	}

	@PostMapping("/query")
	public ResultMessage queryPage(@RequestBody  InfoQueryCondition condition) {
		return ResultMessage.success(infoService.queryPage(condition));
	}

	@GetMapping("/{id}")
	public ResultMessage queryDetail(@PathVariable("id") Long id) {
		HiringInfo detail = infoService.getDetail(id);
		return ResultMessage.success(detail);
	}
}
